package cn.sdack.go.users.controller;

import cn.sdack.go.common.entities.BaseEntity;
import cn.sdack.go.common.entities.JsonResult;
import cn.sdack.go.common.entities.users.RegionEntity;
import cn.sdack.go.users.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sdack
 * @date 2024/2/22
 */
@RestController
public class RegionController {

    @Autowired
    private RegionService regionService;


    @GetMapping("/region/{id}")
    public JsonResult<?> getById(@PathVariable("id") Long id, @RequestParam(value = "tree",defaultValue = "0") Integer child) {
        try {
            RegionEntity entity = regionService.getById(id, child != 0);
            return JsonResult.toJson(entity);
        }catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0,100);
            }
            return JsonResult.toJson(false,message);
        }
    }

    @GetMapping("/region/code/{code}")
    public JsonResult<?> getByCode(@PathVariable("code") Long id, @RequestParam(value = "tree",defaultValue = "0") Integer child) {
        try {
            RegionEntity entity = regionService.getByCode(id, child != 0);
            return JsonResult.toJson(entity);
        }catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0,100);
            }
            return JsonResult.toJson(false,message);
        }
    }

    @GetMapping("/region/level/{level}")
    public JsonResult<?> getByLevel(@PathVariable("level") Integer level, @RequestParam(value = "tree",defaultValue = "0") Integer child) {
        try {
            List<RegionEntity> entity = regionService.getByLevel(level, child != 0);
            return JsonResult.toJson(entity);
        }catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0,100);
            }
            return JsonResult.toJson(false,message);
        }
    }


    @PostMapping("/region")
    public JsonResult<?> add(@RequestBody @Validated(BaseEntity.Add.class) RegionEntity param , BindingResult errors) {
        if (errors.hasErrors()) {
            return JsonResult.toJson(false, errors.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            RegionEntity entity = regionService.add(param);
            return JsonResult.toJson(entity);
        }catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0,100);
            }
            return JsonResult.toJson(false,message);
        }
    }

    @PutMapping("/region")
    public JsonResult<?> edit(@RequestBody @Validated(BaseEntity.Set.class) RegionEntity param , BindingResult errors) {
        if (errors.hasErrors()) {
            return JsonResult.toJson(false, errors.getAllErrors().get(0).getDefaultMessage());
        }

        try {
            RegionEntity entity = regionService.edit(param);
            return JsonResult.toJson(entity);
        } catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0,100);
            }
            return JsonResult.toJson(false,message);
        }
    }


    @DeleteMapping("/region/{id}")
    public JsonResult<?> delById(@PathVariable("id") Long id, @RequestParam(value = "tree",defaultValue = "0") Integer child) {
        try {
            RegionEntity entity = regionService.del(id, child != 0);
            return JsonResult.toJson(entity);
        }catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0,100);
            }
            return JsonResult.toJson(false,message);
        }
    }

}
